| Conditions | 5 |
| Paths | 7 |
| Total Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | // Copyright © 2017 TangDongxin |
||
| 24 | function onParse (result) { |
||
| 25 | |||
| 26 | const chrome = this.chrome || this.browser; |
||
| 27 | const body = document.body; |
||
| 28 | let str; let jsonpMatch; let tag; |
||
| 29 | |||
| 30 | let content = body.textContent; |
||
| 31 | |||
| 32 | dlog(`is relaxed json support: ${isRelaxedJsonSupport}, ${content}`); |
||
| 33 | if (isRelaxedJsonSupport) { |
||
| 34 | |||
| 35 | content = transform(content); |
||
| 36 | |||
| 37 | dlog(`after transform ${content}`); |
||
| 38 | |||
| 39 | } |
||
| 40 | |||
| 41 | |||
| 42 | dlog(`strict mode:${strictOnly}`); |
||
| 43 | if (strictOnly) { |
||
| 44 | |||
| 45 | // Only render when the contentType is json |
||
| 46 | if (/[+\/]json$/i.test(document.contentType)) { |
||
| 47 | |||
| 48 | init(); |
||
| 49 | draw(content, body); |
||
| 50 | |||
| 51 | } |
||
| 52 | |||
| 53 | } else { |
||
| 54 | |||
| 55 | // Check whether the content is json or like json |
||
| 56 | try { |
||
| 57 | |||
| 58 | if (isJSON(content)) { |
||
| 59 | |||
| 60 | init(); |
||
| 61 | draw(content, body); |
||
| 62 | |||
| 63 | } |
||
| 64 | |||
| 65 | } catch (e) { |
||
| 66 | |||
| 67 | dlog(e); |
||
| 68 | |||
| 69 | } |
||
| 70 | |||
| 71 | } |
||
| 72 | |||
| 73 | function init () { |
||
| 74 | |||
| 75 | if (tag) { |
||
| 76 | |||
| 77 | return; |
||
| 78 | |||
| 79 | } |
||
| 80 | |||
| 81 | tag = document.createElement('style'); |
||
| 82 | tag.textContent = [ |
||
| 83 | '.R', ',.D', `{font:${ fontSize } ${ fontStyle }}` + |
||
| 84 | '.D', '{margin-left:0.5em; padding-left:2em; margin-top: 1px; border-left:1px dashed; border-color: #93A1A1;}' + |
||
| 85 | '.X', '{border:1px solid #ccc; padding:1em}' + |
||
| 86 | 'a.L', '{text-decoration:none}' + |
||
| 87 | 'a.L', ':hover,a.L', ':focus{text-decoration:underline}' + |
||
| 88 | 'i.I', '{cursor:pointer;color:#ccc}' + |
||
| 89 | 'i.H', ',i.I', ':hover{text-shadow: 1px 1px 3px #999; color:#333}' + |
||
| 90 | 'i.I', ':before{content:" ▼ "}' + |
||
| 91 | 'i.C', ':before{content:" ▶ "}' + |
||
| 92 | 'i.C', '+.D', '{width:1px; height:1px; margin:0; padding:0; border:0; display:inline-block; overflow:hidden}' + |
||
| 93 | '.S', `{color:${ strColor }}` + // String |
||
| 94 | '.K', `{color:${ keyColor }}` + // Key |
||
| 95 | '.E', '{color:#BCADAD}' + // Error |
||
| 96 | '.B', `{color:${ intColor }}` + // Number and bool |
||
| 97 | '.E', ',.B', '{font-style: italic}' + // Number bold |
||
| 98 | 'h3.E', '{margin:0 0 1em}', |
||
| 99 | ].join(RAND); |
||
| 100 | |||
| 101 | tag.textContent += `i.I${ RAND }:after{content:attr(data-content)}`; |
||
| 102 | tag.textContent += `body {background: ${ bgColor }; color:${ defaultColor };}`; |
||
| 103 | tag.textContent += '.HIDE {width:200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }'; |
||
| 104 | tag.textContent += `* {font:${ fontSize } ${ fontStyle } !important;}`; |
||
| 105 | tag.textContent += '.black_overlay{ display: none; position: fixed; top: 0%; left: 0%; width: 100%; height: 100%; background-color: black; z-index:1001; -moz-opacity: 0.6; opacity:.60; filter: alpha(opacity=60); }'; |
||
| 106 | tag.textContent += '.white_content { display: none; position: fixed; top: 10%; left: 13%; width: 70%; min-width: 500px; min-height: 300px; height: 60%; padding: 1%; border: 16px solid orange; background-color: white; z-index:1002; overflow: auto; }'; |
||
| 117 |